package com.huluobo.a2301monthdemo.pages

import android.util.Log
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Button
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.RadioButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.blankj.utilcode.util.ToastUtils
import com.huluobo.a2301monthdemo.db.MineIntent
import com.huluobo.a2301monthdemo.db.MineViewModel
import com.huluobo.lib_base.BaseLoadingDialog
import com.huluobo.lib_base.StateType
import com.huluobo.lib_base.UIState
import com.huluobo.lib_base.entity.User
import kotlinx.coroutines.launch

/**
 *  Created by LC on 2025/7/10.
 */

@Composable
fun MinePage(navHostController: NavHostController, vm: MineViewModel = hiltViewModel()) {
    var isShowProgress by remember {
        mutableStateOf(false)
    }

    val list = remember {
        SnapshotStateList<User>()
    }

    LaunchedEffect(key1 = "", block = {
        launch {
            vm.state.collect {
                when (it) {
                    is UIState.OnError -> {
                        isShowProgress = false
                        Log.e("TagA", "数据库错误:${it.msg}")
                        ToastUtils.showLong("数据库错误:${it.msg}")
                    }

                    UIState.OnLoading -> {}
                    is UIState.OnSuccess<*> -> {
                        isShowProgress = false
                        when (it.type) {
                            StateType.DEFAULT -> {
                                ToastUtils.showLong("注册成功:${it.data}")
                            }

                            StateType.LOGIN -> {
                                ToastUtils.showLong("登录成功:${(it.data as User).username}")
                            }

                            StateType.QUERY_ALL -> {
                                list.clear()
                                list.addAll(it.data as List<User>)
                            }

                            StateType.DELETE -> {
                                ToastUtils.showLong("删除成功")
                            }

                            else -> {}
                        }
                    }
                }
            }
        }
    })

    var username by remember {
        mutableStateOf("")
    }

    var password by remember {
        mutableStateOf("")
    }

    var role by remember {
        mutableStateOf(1)
    }

    var id by remember {
        mutableStateOf(0L)
    }

    if (isShowProgress) {
        BaseLoadingDialog()
    }

    Column(Modifier.padding(10.dp), verticalArrangement = Arrangement.spacedBy(10.dp)) {
        OutlinedTextField(value = id.toString(), onValueChange = { id = it.toLong() }, Modifier.fillMaxWidth())
        OutlinedTextField(value = username, onValueChange = { username = it }, Modifier.fillMaxWidth())
        OutlinedTextField(value = password, onValueChange = { password = it }, Modifier.fillMaxWidth())
        Row(verticalAlignment = Alignment.CenterVertically) {
            Text(text = "角色 : ")
            RadioButton(selected = role == 1, onClick = {
                role = 1
            })
            Text(text = "经理")
            RadioButton(selected = role == 2, onClick = {
                role = 2
            })
            Text(text = "员工")
        }
        Button(onClick = {
            val user = User(username = username, password = password, role = role)
            vm.sendIntent(MineIntent.Register(user))
        }, modifier = Modifier.fillMaxWidth()) {
            Text(text = "注册")
        }
        Button(onClick = {
            vm.sendIntent(MineIntent.Login(username, password, role))
        }, modifier = Modifier.fillMaxWidth()) {
            Text(text = "登录")
        }
        Button(onClick = {
            vm.sendIntent(MineIntent.QueryAll)
        }, modifier = Modifier.fillMaxWidth()) {
            Text(text = "查询")
        }
        Button(onClick = {
            val user = User(id = id, username = username, password = password, role = role)
            vm.sendIntent(MineIntent.DeleteUser(user))
        }, modifier = Modifier.fillMaxWidth()) {
            Text(text = "删除")
        }

        LazyColumn(content = {
            items(list) {
                Text(text = it.toString())
            }
        })
    }

}